package com.asset.inventory.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.asset.inventory.R
import com.asset.inventory.base.BaseApp
import com.asset.inventory.model.TaskDetailBean
import kotlinx.android.synthetic.main.item_config.view.*
import org.w3c.dom.Text

/**
 * 页面名称：盘点产品适配器
 * 创建时间  2021/03/27 12:55
 */
class ProductAdapter(private val mList: MutableList<TaskDetailBean.Detail>,private val onItemListener: ProductAdapter.OnItemListener): RecyclerView.Adapter<ProductAdapter.ViewHolder>() {


    interface OnItemListener{
        fun onItemClickEvent(position: Int)
        fun onModifyStatusEvent(position: Int)
    }

    class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        val name: TextView = itemView.findViewById(R.id.name)
        val code: TextView = itemView.findViewById(R.id.code)
        val orgName: TextView = itemView.findViewById(R.id.orgName)
        val sn: TextView = itemView.findViewById(R.id.sn)
        val userName: TextView = itemView.findViewById(R.id.userName)
        val locationName: TextView = itemView.findViewById(R.id.locationName)
        val status: Button = itemView.findViewById(R.id.status)
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
        val view = LayoutInflater.from(p0.context).inflate(R.layout.item_product,p0,false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int = mList.size

    @SuppressLint("ResourceAsColor")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val detail = mList[position]
        holder.name.text = detail.name
        holder.code.text = detail.code
        holder.sn.text = detail.serialnumber
        if(!BaseApp.instance.isOnlineModel){
            if(!TextUtils.isEmpty(detail.updateOrgName)){
                holder.orgName.text = detail.updateOrgName
            }else{
                holder.orgName.text = detail.orgName
            }

            if(!TextUtils.isEmpty(detail.updateUserName)){
                holder.userName.text = detail.updateUserName
            }else{
                holder.userName.text = detail.userName
            }

            if(!TextUtils.isEmpty(detail.updateLocationName)){
                holder.locationName.text = detail.updateLocationName
            }else{
                holder.locationName.text = detail.locationName
            }

        }else{
            holder.orgName.text = detail.orgName
            holder.userName.text = detail.userName
            holder.locationName.text = detail.locationName
        }

        when(detail.dutyResultStatus){
            "wait" -> {
                holder.status.text = "待盘"
                holder.status.setBackgroundColor(Color.parseColor("#2260B3"))
            }
            "loss" -> {
                holder.status.text = "盘亏"
                holder.status.setBackgroundColor(Color.parseColor("#FF2413"))
            }
            "equality" -> {
                holder.status.text = "盘平"
                holder.status.setBackgroundColor(Color.parseColor("#34BC70"))
            }


        }

        holder.itemView.setOnClickListener {
            onItemListener.onItemClickEvent(position)
        }

        holder.status.setOnClickListener {
            onItemListener.onModifyStatusEvent(position)
        }
    }

}